﻿using System.Text;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml.Data;

namespace XamlInspector.Model
{
    public static class BindingHelper
    {
        public static string FormatBinding(Binding binding)
        {
            var sb = new StringBuilder();
            sb.Append("{Binding");
            if (binding.Path != null && string.IsNullOrWhiteSpace(binding.Path.Path) == false)
            {
                sb.Append(" " + binding.Path.Path);
            }

            if (binding.Source != null)
            {
                sb.AppendFormat(" Source={0}" + binding.Source);
            }
            else if (binding.RelativeSource != null)
            {
                sb.Append(" RelativeSource={RelativeSource " + binding.RelativeSource.Mode + "}");
            }
            else if (string.IsNullOrWhiteSpace(binding.ElementName) == false)
            {
                sb.Append(" ElementName=" + binding.ElementName);
            }

            if (binding.Converter != null)
            {
                sb.Append(" Converter={StaticResource " + binding.Converter.GetType().Name + "}");
            }

            if (binding.Mode != BindingMode.OneWay)
            {
                sb.Append(" Mode=" + binding.Mode);
            }

            if (binding.ConverterParameter != null)
            {
                sb.Append(" ConverterParameter=" + binding.ConverterParameter);
            }

            sb.Append("}");
            
            return sb.ToString();
        }
    }
}
